layui.use(['form', 'jquery', 'layer'], function () {
    var layer = layui.layer;
    $("#username").blur(function () {
        let username = $("#username").val();
        if (username == "") {
            layer.msg("用户名不能为空！");
            $("#username").focus();
        } else {
            let result = myAjax("/user/getUserByName", "get", {username: username});
            if (result.data != null) {
                layer.msg("该用户名已存在，请重新输入");
                $("#username").focus();
            }
        }
    });
    $("#password").blur(function () {
        let password = $("#password").val();
        if (password==""){
            layer.msg("密码不能为空！");
            $("#password").focus();
        }
    })
    $("#cfmpassword").blur(function () {
        let password = $("#password").val();
        let cfmpassword = $("#cfmpassword").val();
        if (password != cfmpassword) {
            layer.msg("两次密码输入不一致");
            $("#password").focus();
        }
    })
    $("#register").click(function () {
        let username = $("#username").val();
        let password = $("#password").val();
        let cfmpassword = $("#cfmpassword").val();
        let data = {
            code: username,
            password: password
        };
        if (username != "" && password != "" && password == cfmpassword) {
            let result = ajaxModel("/user/register", "post", data);
            if (result.code == 200) {
                layer.alert(result.message, {icon: 1}, function () {
                    window.location.href = "login.html";
                    layer.close();
                })
            } else {
                layer.alert(result.message, {icon: 1});
            }
        } else if (password != cfmpassword) {
            layer.msg("两次密码输入不一致");
            $("#cfmpassword").focus();
        } else
            layer.msg("请完善注册信息！");
    });
    $("#login").click(function () {
        window.location.href = "login.html";
    })
});